# !/usr/bin/env python
# -*- coding: utf-8 -*-
# ///////////////////////////////////////////////////////////////
# @File    :   toExcel.py
# @Time    :   2024/08/17 10:16:59
# @Author  :   kai.luo
# @Version :   1.0.0
# @Desc    :   Save data to an Excel spreadsheet
# ///////////////////////////////////////////////////////////////


def save2Excel(formData):
    """
    将识别的数据保存到excel表格
    Args:
        formData (_type_): 图片表格数据
    """
    table_list = list()
    table_line_list = list()
    tab_header = True
    tab_header_len = 0
    for idx in range(len(formData)):
        # print(idx, data)
        if idx == 0:
            table_line_list.append(formData[idx][1][0])
            continue

        rb_pos_text = formData[idx][0][2][1]  # 右下坐标
        per_rb_pos_text = formData[idx-1][0][2][1]  # 右下坐标
        if rb_pos_text - per_rb_pos_text <= 6:
            table_line_list.append(formData[idx][1][0])
        else:
            if idx == 1 and tab_header:
                tab_header = False
                tab_header_len = len(table_line_list)
            else:
                table_list.append(table_line_list)
                table_line_list = []
                table_line_list.append(formData[idx][1][0])

    # 合并
    if len(table_list[0]) == 1:
        table_list[1][0] = table_list[1][0]+'/'+table_list[0][0]
        table_list.pop(0)

    print(f'table_list----{table_list}')
    # with xw.App(visible=True, add_book=False) as app:
    #     # 创建excel对象
    #     app = xw.App(visible=False, add_book=False)
    #     wb = app.books.add()
    #     # 添加表1
    #     sht1 = wb.sheets.add('图形转表格')
    #     sht1.range('A1').value = table_list[0]
    #     sht1.range('A2').value = table_list[1:]
    #     sht1.range('A1:I200').rows.autofit()
    #     sht1.range('A1:I200').columns.autofit()
    #     wb.save('myExcel.xlsx')  # 保存
    #     app.quit()  # 退出
